package com.csdj.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName: RedisConfig
 * @Author: ZouLi
 * @Description:
 * @Date: 2022/12/23 9:28
 **/
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置String Key
        template.setKeySerializer(new StringRedisSerializer());
        // 设置String Value
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // 设置Hash Key
        template.setHashKeySerializer(new StringRedisSerializer());
        // 设置Hash Value
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // 设置连接工厂
        template.setConnectionFactory(connectionFactory);
        return template;
    }
}
